package com.jservice.api.service.upp;

import com.alibaba.fastjson.JSONObject;
import com.jservice.api.MsgCode;
import com.jservice.api.UppClient;
import com.jservice.api.dto.Request;
import com.jservice.api.dto.Response;
import com.jservice.api.dto.ResponseHeader;
import com.jservice.api.service.BaseService;
import com.jservice.api.service.IService;
import com.jservice.api.smkAppUser.AppUserKit;
import com.jservice.model.SmkAppUser;
import com.wondersgroup.upp.api.core.UPPlatformRequest;
import org.springframework.stereotype.Service;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

/**
 * Upp2001 - 账户可提现金额查询(车改账户)
 *
 * @Author cwledit
 * @DATE 2017/6/14.
 */
@Service("upp2001")
public class Upp2001 extends BaseService implements IService{

    private String SERVER_NAME = "upp.trade.withdrawal.balance.query";

    private static final Map<String,String> bank_type;
    static {
        bank_type = new HashMap<>();
        bank_type.put("1001","中国工商银行");
        bank_type.put("1002","中国农业银行");
        bank_type.put("1003","中国建设银行");
        bank_type.put("1004","中国银行");
        bank_type.put("1005","交通银行");
        bank_type.put("1006","温州银行");
        bank_type.put("1007","浙江省农信联社");
        bank_type.put("1008","中国邮政储蓄银行");
    }

    @Override
    public Response excute(Request request, Response response, ResponseHeader header, String method) throws IOException {
        Map paramMap =  request.getData();
        String cardNo = paramsStringFilter(paramMap.get("card_no"));
        SmkAppUser smkAppUser = AppUserKit.getCurrent(request);
        if (smkAppUser == null){
            return renderFAIL(MsgCode.Code_1007,response,header);
        }
        Map data = new HashMap();
//        data.put("id_type","01");
//        data.put("id_no",smkAppUser.getIdCard());
//        data.put("id_no","330322196206083613");
        data.put("card_no",cardNo);
        data.put("account_type","06");


        UPPlatformRequest uRequest = getUPRequest(SERVER_NAME,request.getHeader());
        uRequest.setBiz_content(sortParam(data));
        UppClient client = UppClient.getInstance();
        JSONObject jsonObject = client.doRequest(uRequest);
        if ("000000".equals(jsonObject.getString("code"))) {
            jsonObject.remove("code");
            jsonObject.put("bank_type_name",bank_type.get(jsonObject.getString("bank_type")));
            return renderSUC(jsonObject,response,header);
        }else{
            return renderFAIL(MsgCode.Code_1009,"市民卡网关返回异常:"+jsonObject.getString("msg"),response,header);
        }
    }
}
